/**
 * 
 */
package fr.isima.isipaint.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JColorChooser;
import javax.swing.JFrame;

import fr.isima.isipaint.controllers.DrawPanelObjectController;
import fr.isima.isipaint.controllers.ToolsController;
import fr.isima.isipaint.model.Scene;
import fr.isima.isipaint.tools.FillAction;
import fr.isima.isipaint.tools.OvalAction;
import fr.isima.isipaint.tools.RectangleAction;
import fr.isima.isipaint.tools.SelectionAction;
import fr.isima.isipaint.tools.ToolBox;

/**
 * @author daniel
 * 
 */
public final class MainWindow extends JFrame {

	/** Serial number */
	private static final long serialVersionUID = 1L;

	/** The drawing Panel */
	private DrawPanel area;

	/** */
	private JColorChooser colorChooser;

	/** The status bar */
	private StatusBar status = new StatusBar();

	/** The toolbar */
	private ToolBar toolBar;

	/** The toolbox */
	private ToolBoxPanel toolBox;

	/** The area listenner */
	private DrawPanelObjectController areaListener;

	/** The tool box and bar listenner */
	private ToolsController toolsController;

	/** The model */
	private Scene scene;

	/**
	 * 
	 * @param scene
	 *            The model
	 */
	public MainWindow(Scene scene) {
		this.scene = scene;
		// Window features
		setPreferredSize(new Dimension(400, 500));
		setBackground(Color.WHITE);
		setLayout(new BorderLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		

		// window component
		pack();
		colorChooser = new JColorChooser(Color.BLACK);
		areaListener = new DrawPanelObjectController(scene);
		toolsController = new ToolsController(scene);
		area = new DrawPanel(scene, areaListener);
		status.setBackground(Color.BLUE);
		status.setText("Status");

		initToolBar();
		initToolBox();
		setJMenuBar(new MenuBar(toolsController));
		addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowActivated(WindowEvent arg0) {
				resizeGroup();
			}
		});

		// Position
		getContentPane().add(area, BorderLayout.CENTER);
		getContentPane().add(status, BorderLayout.SOUTH);
		getContentPane().add(toolBox, BorderLayout.WEST);
		getContentPane().add(toolBar, BorderLayout.NORTH);

		// Run
		setVisible(true);
	}

	/* ======================================================= */
	/**
	 * Load the possibles action to the tool box
	 */
	/* ======================================================= */

	private void initToolBox() {
		ToolBox tools = new ToolBox();
		tools.addTool(new SelectionAction());
		tools.addTool(new RectangleAction());
		tools.addTool(new OvalAction());
		tools.addTool(new FillAction());
		toolBox = new ToolBoxPanel(tools, toolsController);
	}

	/* ======================================================= */
	/**
	 * Load the possibles action to the tool bar
	 */
	/* ======================================================= */

	private void initToolBar() {
		ToolBox tools = new ToolBox();
		tools.addTool(new RectangleAction());
		tools.addTool(new OvalAction());
		tools.addTool(new FillAction());
		toolBar = new ToolBar(tools, toolsController);
	}

	/**
	 * Resize the main group to draw it correctly
	 */
	public void resizeGroup() {
		scene.getGroup().setBounds(0,0,scene.getGroup().getParent().getWidth(), scene.getGroup().getParent().getHeight());		
	}
}
